package com.example.bubble;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.example.piccut.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BubbleViewLike extends FrameLayout {
    private Paint mPaint;
    private int mWidth;
    private int mHeight;
    private List<Integer> mBubbleIcons = new ArrayList<>();

    public BubbleViewLike(Context context) {
        super(context);
        init();
    }

    public BubbleViewLike(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public BubbleViewLike(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimatorBean b = new AnimatorBean();
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 100f);
                valueAnimator.addUpdateListener(b);
                valueAnimator.addListener(b);

//                valueAnimator.setDuration((int) (2100 + Math.random() * 700));
                valueAnimator.setDuration(3000);
                valueAnimator.start();
                Log.i("cjztest", "click..");
            }
        });
        mPaint = new Paint();
        mPaint.setStrokeWidth(4);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mBubbleIcons.add(R.drawable.bubble_awesome);
        mBubbleIcons.add(R.drawable.bubble_champ_cup);
        mBubbleIcons.add(R.drawable.bubble_cow);
        mBubbleIcons.add(R.drawable.bubble_gift);
        mBubbleIcons.add(R.drawable.bubble_hear);
        mBubbleIcons.add(R.drawable.bubble_love);
        mBubbleIcons.add(R.drawable.bubble_red_pack);
        mBubbleIcons.add(R.drawable.bubble_rocket);

//        Button a = new Button(this.getContext());
//        a.setText("asdasdasd");
//        addView(a);
    }

    private class AnimatorBean implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
        private View mButton;
        private double amplitude;
        private double offset;
        private double freq;

        @Override
        public void onAnimationStart(Animator animation, boolean isReverse) {
            amplitude = Math.random() * mWidth / 2;
            offset = Math.random() * 90;
            freq = 180f + Math.random() * 180f;

            mButton = new Button(getContext());
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(120, 120);
            mButton.setLayoutParams(params);
            mButton.setX(mWidth / 2);
            mButton.setY(mHeight);
            mButton.setBackgroundResource(mBubbleIcons.get(new Random().nextInt(mBubbleIcons.size())));
            addView(mButton);
            Log.i("cjztest", "shit: onAnimationStart, isReverse:" + isReverse);
        }

        @Override
        public void onAnimationEnd(Animator animation, boolean isReverse) {
            removeView(mButton);
            Log.i("cjztest", "shit: onAnimationEnd");
        }

        @Override
        public void onAnimationStart(Animator animation) {
            if (Build.VERSION.SDK_INT <= 22) {
                onAnimationStart(animation, false);
            }
            Log.i("cjztest", "shit: onAnimationStart2:" + Build.VERSION.SDK_INT);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (Build.VERSION.SDK_INT <= 22) {
                onAnimationEnd(animation, false);
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            if (mButton != null) {
                float curVal = (float) animation.getAnimatedValue();
                float scale = (50f - Math.abs(50f - curVal)) / 50f;
                float y = ((1 - curVal / 100f) * mHeight);
                double x = (Math.sin(Math.toRadians(freq * (1 - curVal / 100f) + offset))) * amplitude; //频率控制震荡幅度

                mButton.setX((float) x + mWidth / 2);
                mButton.setY(y);

                mButton.setScaleX(scale * 1.6f);
                mButton.setScaleY(scale * 1.6f);
                mButton.setAlpha(scale);
                Log.i("cjztest", "shit: onAnimationUpdate");
                invalidate();
            }
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = MeasureSpec.getSize(heightMeasureSpec);
        if (w != mWidth || h != mHeight) { //已经onMeasuer过一次，除非界面大小改动否则不重新初始化view
            mWidth = w;
            mHeight = h;
        }
    }


//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
////        canvas.drawColor(Color.RED);
////        Path path = new Path();
////        path.cubicTo(0,0, canvas.getWidth(),canvas.getHeight() / 2, 0, canvas.getHeight());
//////        path.quadTo(0,0, canvas.getWidth(),canvas.getHeight() / 2);
//////        path.quadTo(canvas.getWidth(),canvas.getHeight() / 2, 0, canvas.getHeight());
////
////        canvas.drawPath(path, mPaint);
//    }
}
